﻿using LF.Domain.Entities;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Bcpg;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core.Tokenizer;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace LF.Application.Helpers
{
    public class ApiHelper
    {
        public async static Task<JsonNode?> GetAsync(string baseUrl, string url, Dictionary<string, object>? data = null)
        {
            var paras = string.Empty;
            if (data != null)
            {
                foreach (var item in data)
                {
                    paras += (paras == string.Empty ? "?" : "&") + item.Key + "=" + item.Value;
                }
            }
            using (HttpClient client = new())
            {
                client.BaseAddress = new Uri(baseUrl);

                var r = await client.GetStringAsync(url + paras);
                if (string.IsNullOrEmpty(r))
                    return null;
                JsonNode? json = JsonObject.Parse(r);
                return json;
            }

        }

        public async static Task<JsonNode?> PostFormDataAsync(string baseUrl, string url, Dictionary<string, object>? data = null)
        {
            using (var formdata = new MultipartFormDataContent())
            {
                if(data!=null)
                    foreach(var item in data)
                    {
                        formdata.Add(new StringContent(item.Value?.ToString() ?? ""), item.Key);
                    }
                using (HttpClient client = new())
                {
                    client.BaseAddress = new Uri(baseUrl);
                    var r = await client.PostAsync(url, formdata);
                    var result = await r.Content.ReadAsStringAsync();
                    if (string.IsNullOrEmpty(result))
                        return null;
                    JsonNode? json = JsonObject.Parse(result);
                    return json;
                }
            }

        }

        public async static Task<JsonNode?> PostAsync(string baseUrl, string url, Dictionary<string, object>? data = null)
        {
            using (HttpClient client = new())
            {
                client.BaseAddress = new Uri(baseUrl);
                var content = new StringContent(JsonSerializer.Serialize(data));
                var r = await  client.PostAsync(url, content);
                var result = await  r.Content.ReadAsStringAsync();
                if (string.IsNullOrEmpty(result))
                    return null;
                JsonNode? json = JsonObject.Parse(result);
                return json;
            }

        }

        public async static Task<JsonObject?> PostOATokenAsync(string url,object? data = null)
        {
            using (HttpClient client = new())
            {
                client.BaseAddress = new Uri(LFConfig.OaUrl);
                client.DefaultRequestHeaders.Add("appid", "EEAA5436-7577-4BE0-8C6C-89E9D88805AA");
                client.DefaultRequestHeaders.Add("secret", "fHtUekV33sL9Xp4gktolnEcz9CLykNme1Dk6OF/5hhdQzNL8nQAOQeYEiGRsynMILVRvfiU3uzLsTz7amXvbN8+1uLrwA9Ri2qMHYtVQfcywCWMhNLjksgBw8ZFzyfK/d1ss7CjmE3g/7Sibw0AsBNCSYq0XQclgBxhjtSsG3letgQLmGJrb7GzZ9VzLJ0Y4ID5WKGgh3aK4IyDc2NwLCyJ6U0AwlrQDedKizc1r4Gsbfyg+Z2QUr38lkPawGFGQGz3DSSY4YWQuIbwkbHy9ZKG8Bf5Fkf1jfYCtqBrPw/7RKRhjypv3EyxHgg9TB8iyIIYfSfWRF5Yr3grXSLV3Rg==");
                var content = new StringContent(JsonSerializer.Serialize(data));
                var r = await  client.PostAsync(url, content);
                var result = await  r.Content.ReadAsStringAsync();
                if (string.IsNullOrEmpty(result))
                    return null;
                JsonObject json = (JsonObject)JsonObject.Parse(result)!;
                return json;
            }

        }
        public async static Task<JsonObject?> PostOACreateAsync(string url, string appid,string token, string userid, Dictionary<string,object>? data = null  )
        {

            
            using (HttpClient client = new())
            {
                client.BaseAddress = new Uri(LFConfig.OaUrl);
                client.DefaultRequestHeaders.Add("appid", appid);
                client.DefaultRequestHeaders.Add("token", token);
                client.DefaultRequestHeaders.Add("userid", userid);

                FormUrlEncodedContent? content = null;
                if (data != null)
                {
                    var list = new List<KeyValuePair<string, string>>();
                    foreach (var item in data)
                    {
                        var v = string.Empty;
                        if(item.Value is JsonObject)
                        {
                            v = JsonSerializer.Serialize(item.Value);
                        }
                        else
                        {
                            v = item.Value.ToString();
                        }

                        list.Add(new KeyValuePair<string, string>(item.Key,v!));
                    }
                    content = new FormUrlEncodedContent(list);
                }

                var response = await client.PostAsync(url, content);
                var result = await response.Content.ReadAsStringAsync();
                if (string.IsNullOrEmpty(result))
                    return null;
                JsonObject jsonObject = (JsonObject)JsonObject.Parse(result)!;
                return jsonObject;
            }

        }
        public async static Task<Stream?> Post2Async(string baseUrl, string url, Dictionary<string, object>? data = null)
        {
            using (HttpClient client = new())
            {
                client.BaseAddress = new Uri(baseUrl);
                var content = new StringContent(JsonSerializer.Serialize(data));
                var r = await client.PostAsync(url, content);
                if (r.StatusCode == System.Net.HttpStatusCode.OK) {
                    var result = await r.Content.ReadAsStreamAsync();
                    return result;
                }
                return null;
            }

        }

        public async static Task<JsonArray?> PostOAUserId( string url, object? data = null)
        {
            using (HttpClient client = new())
            {
                client.BaseAddress = new Uri(LFConfig.OaUrl);
                var content = new StringContent(JsonSerializer.Serialize(data),Encoding.UTF8, "application/json");
                var r = await client.PostAsync(url, content);
                if (r.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var result = await r.Content.ReadAsStreamAsync();
                    if (result!=null)
                    {
                        JsonArray jsonArray = (JsonArray)JsonArray.Parse(result)!;
                        return jsonArray;

                    }
                    else       
                        return null;
                }
                return null;
            }

        }
    }
}
